package com.kexun.webSocket;

import com.alibaba.fastjson.JSON;
import com.kexun.entity.DO.Manage;
import com.kexun.handler.ApiRRException;
import com.kexun.mapper.ManageMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

@Component
public class MessageHandler {
    @Autowired
    private ManageMapper manageMapper;

    @Autowired
    private WebSocketServer webSocketServer;

    public void sendMessage(String user, String content) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("from", "system");
        map.put("content", content);
        map.put("type", 2);//其他消息
        String msg = JSON.toJSONString(map);
        if (StringUtils.isEmpty(user)) {
            List<Manage> manages = manageMapper.selectList(null);
            for (Manage manage : manages) {
                if (manage.getOpenPush() == 0) {
                    webSocketServer.sendInfo(msg, manage.getManageName());
                }
            }
        } else {
            webSocketServer.sendInfo(msg, user);
        }
    }


    public void retrySendMessage(String user, String content) {
        if (StringUtils.isEmpty(user)) {
            throw new ApiRRException("用户不能为空");
        }
        webSocketServer.retrySend(content, user);
    }

    public void sendMessage(int shop, String content) {
        try {
            List<Manage> manageList = manageMapper.findManageListByShopVal(shop);
            HashMap<String, Object> map = new HashMap<>();
            map.put("from", "system");
            map.put("content", content);
            map.put("type", 1);//订单提示
            String msg = JSON.toJSONString(map);
            for (Manage manage : manageList) {
                if (manage.getOpenPush() == 0) {
                    webSocketServer.sendInfo(msg, manage.getManageName());
                }
            }
        } catch (Exception e) {

        }
    }


}
